﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using projet_fichier_objet;
using FichierTxt;
using Outils;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace App_Recherche
{
    public partial class Edition : System.Web.UI.Page
    {
        private string id = string.Empty;
        private string path = string.Empty;
        private Personne Interimaire = new Personne();
        private int Id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            /*Lors du premier chargement de la page
             * les textbox sont remplis automatiquement 
             * avec les informations de la page "resultat.aspx
             * Or lors du second chargement ceux ci ont
             * été modifiés par l'utilisateur et donc plus aucun 
             * intérêt de les remplir 
             */
            this.id = Request.QueryString["id"];
            if (Page.IsPostBack == false) //premier chargement
            {
                if (int.TryParse(id, out Id))
                {
                    //string CnxInterimaire = "Data Source=PILOTT-LGZ;Initial Catalog=Interimaires;Integrated Security=True;Pooling=False";
                    SqlConnection SqlC = new SqlConnection(Utils.CnxInterimaire);

                    SqlC.Open();
                    SqlCommand SQLCmd = SqlC.CreateCommand();
                    SQLCmd.CommandText = "SELECT * FROM  Interimaire WHERE Id = '" + this.Id + "'";
                    SqlDataReader SqlReader = SQLCmd.ExecuteReader();

                    while (SqlReader.Read() && SqlReader != null)
                    {
                        Personne Interimaire = new Personne();
                        Nomedit.Text = (string)SqlReader["Nom"];
                        Emailedit.Text = (string)SqlReader["Email"];
                        Villeedit.Text = (string)SqlReader["Ville"];
                        Dateedit.Text = SqlReader["DateNaiss"].ToString();
                    }
                    SqlC.Close();
                }
            }
            else //postback
            {

            }
        }
        protected void submit_Click(object sender, EventArgs e)
        {
            if (int.TryParse(id, out Id)) //convertion en entier
            {
                //L'id ne change pas seul les autres champ sont modifiables
                Interimaire.ID = this.Id;
                bool Erreur = Utils.CheckSaisi(Nomedit.Text);
                if (Erreur == false)
                {
                    MessageNom.Text = string.Empty;
                    for (int i = 0; i < Emailedit.Text.Length; i++)
                    {
                        if (Emailedit.Text[i] == '@')
                        {
                            MessageEmail.Text = string.Empty;
                            i = Emailedit.Text.Length + 1;
                        }
                        else
                            MessageEmail.Text = "saisie incorrecte";
                    }
                    DateTime Madate = new DateTime();
                    Madate.ToString("d");
                    bool SaisieError = DateTime.TryParse(Dateedit.Text, out Madate);

                    if (SaisieError == true)
                    {
                        //Interimaire.DateNaissance = Madate;
                        MessageDate.Text = string.Empty;

                        //string CnxInterimaire = "Data Source=PILOTT-LGZ;Initial Catalog=Interimaires;Integrated Security=True;Pooling=False";
                        SqlConnection SqlC = new SqlConnection(Utils.CnxInterimaire);
                        //UPDATE
                        SqlC.Open();
                        SqlCommand SQLCmd = SqlC.CreateCommand();
                        SQLCmd.CommandText = "UPDATE Interimaire SET Nom = '" + Nomedit.Text + "', Email = '" + Emailedit.Text + "', Ville = '" + Villeedit.Text + "',Agence = '"+ ListeAgence.SelectedValue + "', DateNaiss = '" + Dateedit.Text + "' WHERE Id = '" + Id + "'";
                        int nbligne = SQLCmd.ExecuteNonQuery();

                        Response.Redirect("~/Page_aspx/Default.aspx?id=" + (int)Utils.TypePage.edition + "&Aff=" + nbligne);
                        
                    }
                    else
                        MessageDate.Text = "saisie incorrecte";
                }
                else
                {
                    MessageNom.Text = "Saisie incorecte";
                }
            }
        }

        protected void reset_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Page_aspx/Default.aspx");
        }
    }
}
